<template>
  <el-dialog
    class="ba-operate-dialog"
    :close-on-click-modal="false"
    :model-value="baTable.form.operate ? true : false"
    @close="baTable.toggleForm"
  >
    <template #header>
      <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']">
        {{ baTable.form.operate !== "edit" ? "添加" : "编辑" }}
      </div>
    </template>
    <el-scrollbar
      v-loading="baTable.form.loading"
      class="ba-table-form-scrollbar"
    >
      <div
        class="ba-operate-form"
        :class="'ba-' + baTable.form.operate + '-form'"
        :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
      >
        <el-form
          ref="formRef"
          @keyup.enter="baTable.onSubmit(formRef)"
          :model="baTable.form.items"
          label-position="right"
          :label-width="baTable.form.labelWidth + 'px'"
          :rules="rules"
          v-if="!baTable.form.loading"
        >
          <el-form-item prop="name" label="告警策略名称">
            <el-input
              v-model="baTable.form.items!.name"
              type="string"
              placeholder="告警策略名称"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="策略类型" prop="type">
            <el-input
              v-model="baTable.form.items!.type"
              type="string"
              placeholder="策略类型"
            ></el-input>
          </el-form-item> -->
          <el-form-item label="策略类型" prop="type">
            <el-select
              v-model="baTable.form.items!.type"
              placeholder="请选择策略类型"
            >
              <el-option
                v-for="item in rule.typeType"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictLabel"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="策略启停" prop="flag">
            <el-select
              v-model="baTable.form.items!.flag"
              placeholder="请选择策略启停"
            >
              <el-option
                v-for="item in rule.typeFlag"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictValue - 0"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否开启远程控制" prop="remoteControl">
            <el-select
              v-model="baTable.form.items!.remoteControl"
              placeholder="请选择是否开启"
            >
              <el-option
                v-for="item in rule.typeControl"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictValue - 0"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="通知配置" prop="noticeConfigId">
            <el-select
              v-model="baTable.form.items!.noticeConfigId"
              placeholder="请选择通知配置"
            >
              <el-option
                v-for="item in state.noticeOptions"
                :key="item.noticeConfigId"
                :label="item.noticeConfigName"
                :value="item.noticeConfigId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="baTable.form.items!.remark"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 5 }"
              placeholder="备注"
            />
          </el-form-item>
        </el-form>
      </div>
    </el-scrollbar>
    <template #footer>
      <div
        :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'"
      >
        <el-button @click="baTable.toggleForm('')">取消</el-button>
        <el-button
          v-blur
          :loading="baTable.form.submitLoading"
          @click="baTable.onSubmit(formRef)"
          type="primary"
        >
          {{
            baTable.form.operateIds &&
            (baTable.form.operateIds.length > 1 ||
              (baTable.form.operateIds as TableRow).parentId)
              ? "保存并编辑下一项"
              : "保存"
          }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import type { TableRow } from "@/utils/baTable"; //表格列行类型
import { reactive, ref, inject, onMounted } from "vue";
import type baTableClass from "@/utils/baTable";
import type { ElForm, FormItemRule, FormRules } from "element-plus";

const formRef = ref<InstanceType<typeof ElForm>>();
const baTable = inject("baTable") as baTableClass;
const rule = inject("rule") as any;
const noticeConfig = inject("noticeConfig") as any;
// const itemWarning = inject("itemWarning") as any;

const state: {
  regionOptions: anyObj[];
  categoriesOptions: anyObj[];
  gatewayOptions: anyObj[];
  objectOptions: anyObj[];
  noticeOptions: anyObj[];
} = reactive({
  regionOptions: [], //区域列表
  categoriesOptions: [], //告警类别列表
  gatewayOptions: [], //告警类别列表
  objectOptions: [], //告警对象列表
  noticeOptions: [], //告警配置对象列表
});

const props = {
  label: "areaName",
  value: "areaId",
  children: "children",
};

const rules = reactive<FormRules>({
  name: [{ required: true, message: "告警策略名称不能为空", trigger: "blur" }],
  flag: [{ required: true, message: "策略启停不能为空", trigger: "blur" }],
  remoteControl: [
    { required: true, message: "是否开启远程控制不能为空", trigger: "blur" },
  ],
  noticeConfigId: [
    { required: true, message: "通知配置不能为空", trigger: "blur" },
  ],
});

//选择告警类别
const objectChange = (val: any) => {
  baTable.form.items!.categoryName = val.categoryName;
  console.log("选择告警类别", val, baTable.form.items);
};

//获取告警通知配置列表
const getNoticeConfigList = () => {
  noticeConfig
    .getWarningNoticeConfigList({ pageSize: 10000 })
    .then((res: any) => {
      state.noticeOptions = res;
    })
    .catch((err: any) => {});
};

onMounted(() => {
  getNoticeConfigList(); //获取告警通知配置列表
});
</script>
<style lang="scss" scoped>
.el-scrollbar {
  overflow: auto;
  position: relative;
  height: 100%;

  .el-scrollbar__wrap {
    overflow: auto;
    height: 100%;
  }
}
</style>
